package be.dch.typeditor.domain;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import be.dch.gui.i18n.LanguageManager;

public enum Language {
    UNSPECIFIED(0x00, Locale.getDefault().getLanguage(), "supportedlang.unspecified"), //
    FRENCH(0x01, "fr", "supportedlang.french"), //
    GERMAN(0x02, "de", "supportedlang.german"), //
    DUTCH(0x03, "nl", "supportedlang.dutch"), //
    ENGLISH(0x04, "en", "supportedlang.english"), //
    ITALIAN(0x05, "it", "supportedlang.italian"), //
    FINNISH(0x06, "fi", "supportedlang.finnish"), //
    SWEDISH(0x07, "sv", "supportedlang.swedish"), //
    SPANISH(0x08, "es", "supportedlang.spanish"), //
    BASQUE(0x09, "eu", "supportedlang.basque"), //
    CATALAN(0x0a, "ca", "supportedlang.catalan"), //
    GALICIAN(0x0b, "gl", "supportedlang.galician"), //
    WELSH(0x0c, "cy", "supportedlang.welsh"), //
    GAELIC(0x0d, "gd", "supportedlang.gaelic"), //
    DANISH(0x0e, "da", "supportedlang.danish"), //
    NORWEGIAN(0x0f, "no", "supportedlang.norwegian"), //
    PORTUGUESE(0x10, "pt", "supportedlang.portuguese"), //
    SLOVAK(0x11, "sk", "supportedlang.slovak"), //
    CZECH(0x12, "cs", "supportedlang.czech"), //
    CROATIAN(0x13, "hr", "supportedlang.croatian"), //
    HUNGARIAN(0x14, "hu", "supportedlang.hungarian"), //
    POLISH(0x15, "pl", "supportedlang.polish"), //
    TURKISH(0x16, "tr", "supportedlang.turkish"), //
    GREEK(0x17, "el", "supportedlang.greek"), //
    SLOVENIAN(0x18, "sl", "supportedlang.slovenian"), //
    RUSSIAN(0x19, "ru", "supportedlang.russian"), //
    ESTONIAN(0x1a, "et", "supportedlang.estonian"), //
    LATVIAN(0x1b, "lv", "supportedlang.latvian"), //
    ROMANIAN(0x1c, "ro", "supportedlang.romanian"), //
    ALBANIAN(0x1d, "sq", "supportedlang.albanian"), //
    BOSNIAN(0x1e, "bs", "supportedlang.bosnian"), //
    LITHUANIAN(0x1f, "lt", "supportedlang.lithuanian"), //
    SERBIAN(0x20, "sr", "supportedlang.serbian"), //
    MACEDONIAN(0x21, "mk", "supportedlang.macedonian"), //
    BULGARIAN(0x22, "bg", "supportedlang.bulgarian");

    private static Map<Integer, Language> lookup;
    private int langCode;
    private String i18nkey;
    private Locale locale;

    static {
	Language.lookup = new HashMap<Integer, Language>();
	for (final Language lang : EnumSet.allOf(Language.class)) {
	    Language.lookup.put(lang.langCode, lang);
	}
    }

    private Language(final int languageCode, final String language, final String i18nkey) {
	this.langCode = languageCode;
	this.i18nkey = i18nkey;
	this.locale = new Locale(language);
    }

    public String toTypFileValue() {
	return this.name().toLowerCase();
    }

    @Override
    public String toString() {
	return LanguageManager.translate(this.i18nkey);
    }

    public static Language getLanguage(final Locale locale) {
	Language result = null;
	final List<Language> values = new ArrayList<Language>(Language.lookup.values());
	for (final Language language : values) {
	    if (locale.getLanguage() == language.locale.getLanguage()) {
		result = language;
		break;
	    }
	}
	return result;

    }

    public static Language[] getValues() {
	final Language[] langs = Language.values();
	Arrays.sort(langs, new Comparator<Language>() {
	    public int compare(final Language source, final Language target) {
		return source.toString().compareTo(target.toString());
	    }
	});
	return langs;
    }

}
